﻿using gfLibrary;
using IYAKASAPortal.Services;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using CrystalDecisions.CrystalReports.Engine;
using DBUtility;
using Model;

namespace IYAKASAPortal.PrintAll
{
    public partial class PrintAJYQ : System.Web.UI.Page
    {
        private UserInfo userInfo { get; set; }
        private ReportDocument document;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] != null && Session["UserName"].ToString() != "")
            {
                string userName = Session["UserName"].ToString();
                userInfo = IYAKASAPortal.Services.UserAuthority.GetUserAuthority(userName);
            }
            else
            {
                gfJs.RunFunction(this.Page, "parent.window.location = '../logout.aspx';");
            }

            if (Request["UniqueKey"] != null && Request["UniqueKey"].ToString() != "")
            {
                BindReportData(Request["UniqueKey"].ToString());
            }
            else
            {
                gfJs.RunFunction(this.Page, "parent.window.location = '../logout.aspx';");
            }

            RESULT_VEHICLE_INFO_BLL BLL = new RESULT_VEHICLE_INFO_BLL();
            var INFO = BLL.GetModelList("ID=" + Request["UniqueKey"].ToString());
            string AJLSH = INFO[0].JCLSH;
            txtAJLSH.Value = AJLSH;

            ShowBtnWitnLiceNo(btnUpload, "08,");
            ShowBtnWitnLiceNo(btnReCheck, "08,");

            if (INFO[0].JCXH == 6)
            {
                ShowBtnWitnLiceNo(btnReCheckMoto, "15,");
            }  
        }

        /// <summary>
        /// 根据权限编号显示按钮
        /// </summary>
        /// <param name="btn"></param>
        /// <param name="licenseNo"></param>
        private void ShowBtnWitnLiceNo(Button btn, string licenseNo)
        {
            try
            {
                if (Session["UserName"] != null && Session["UserName"].ToString() != "")
                {
                    string userName = Session["UserName"].ToString();
                    userInfo = IYAKASAPortal.Services.UserAuthority.GetUserAuthority(userName);
                    if (userInfo.UserRole.Contains(licenseNo))
                    {
                        btn.Visible = true;
                    }
                    else
                    {
                        btn.Visible = false;
                    }
                }
                else
                {
                    gfJs.RunFunction(this.Page, "parent.window.location = '../logout.aspx';");
                }
            }
            catch (Exception ex)
            {
            }

        }

        private void BindReportData(string strKey)
        {
            string strPath = Server.MapPath("~/rpt/CrystalReportAJYQ.rpt");
            SetCRDataSource setData = new SetCRDataSource();

            //document = setData.BindAJYQReport(strKey, strPath, chkSingle.Checked);
            document = setData.BindAJYQReportEx(strKey, strPath, chkSingle.Checked);
            CRAJYQ.ReportSource = document;
            CRAJYQ.DataBind();
            CRAJYQ.RefreshReport();
        }

        protected void btnAJ_Click(object sender, EventArgs e)
        {
            if (Request["UniqueKey"] != null && Request["UniqueKey"].ToString() != "")
            {
                Response.Redirect("PrintAJ.aspx?UniqueKey=" + Request["UniqueKey"].ToString());
            }
            else
            {
                gfJs.RunFunction(this.Page, "parent.window.location = '../logout.aspx';");
            }
        }

        protected void btnRG_Click(object sender, EventArgs e)
        {
            if (Request["UniqueKey"] != null && Request["UniqueKey"].ToString() != "")
            {
                Response.Redirect("PrintAJRG.aspx?UniqueKey=" + Request["UniqueKey"].ToString());
            }
            else
            {
                gfJs.RunFunction(this.Page, "parent.window.location = '../logout.aspx';");
            }
        }

        protected void btnYQ_Click(object sender, EventArgs e)
        {
            if (Request["UniqueKey"] != null && Request["UniqueKey"].ToString() != "")
            {
                BindReportData(Request["UniqueKey"].ToString());
            }
        }

        protected void chkSingle_CheckedChanged(object sender, EventArgs e)
        {
            btnYQ_Click(sender, e);
        }

        protected override void OnUnload(EventArgs e)
        {
            document.Dispose();
            base.OnUnload(e);
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string strJCLSH = "";
            if (Request["UniqueKey"] != null && Request["UniqueKey"].ToString() != "")
            {
                RESULT_VEHICLE_INFO_BLL vehicleInfo = new RESULT_VEHICLE_INFO_BLL();
                strJCLSH = vehicleInfo.GetModelList("ID=" + Request["UniqueKey"].ToString())[0].JCLSH;

                UploadNetInfos uploadInfo = new UploadNetInfos();
                string userName = Session["UserName"].ToString();
                userInfo = IYAKASAPortal.Services.UserAuthority.GetUserAuthority(userName);

                bool bUpload = uploadInfo.UploadNetInformation(strJCLSH, userInfo.UserName);

                string str = string.Empty;
                if (bUpload)
                {
                    //gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "上传总结论成功！");
                    str = "if(confirm('上传总结论成功，是否关闭当前页面？')){window.close();}";

                }
                else
                {
                    str = "if(confirm('上传总结论失败，是否关闭当前页面？')){window.close();}";
                }
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "uuu", str, true);
            }
        }

        /// <summary>
        /// 把数据异常的报告单的总判定改为 不合格 再上传
        /// </summary>
        /// <param name="strJCLSH"></param>
        /// <returns></returns>
        private bool AlterZ_PD(string strJCLSH)
        {
            bool succ = false;
            string strSQL = string.Format("UPDATE RESULT_VEHICLE_INFO SET Z_PD = 2 WHERE JCLSH = '{0}'  ", strJCLSH);
            succ = DbHelper.ExecuteSql(strSQL);
            return succ;
        }

        private void UploadResult(string strJCLSH)
        {
            UploadNetInfos uploadInfo = new UploadNetInfos();
            string userName = Session["UserName"].ToString();
            userInfo = IYAKASAPortal.Services.UserAuthority.GetUserAuthority(userName);

            bool bUpload = uploadInfo.UploadNetInformation(strJCLSH, userInfo.UserName);

            string str = string.Empty;
            if (bUpload)
            {
                //gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "上传总结论成功！");
                str = "if(confirm('上传总结论成功，是否关闭当前页面？')){window.close();}";

            }
            else
            {
                str = "if(confirm('上传总结论失败，是否关闭当前页面？')){window.close();}";
            }
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "uuu", str, true);

        }

        protected void btnReCheck_Click(object sender, EventArgs e)
        {
            string strJCLSH = "";
            if (Request["UniqueKey"] != null && Request["UniqueKey"].ToString() != "")
            {
                RESULT_VEHICLE_INFO_BLL vehicleInfo = new RESULT_VEHICLE_INFO_BLL();
                strJCLSH = vehicleInfo.GetModelList("ID=" + Request["UniqueKey"].ToString())[0].JCLSH;
                AlterZ_PD(strJCLSH);
                UploadResult(strJCLSH);
            }
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                string strUniqueKey = Request["UniqueKey"];
                if ((strUniqueKey == null) || (strUniqueKey.Length == 0) || (gfValidate.Is_Integer(strUniqueKey) == false))
                {
                    return;
                }
                RESULT_VEHICLE_INFO_BLL bll = new RESULT_VEHICLE_INFO_BLL();
                RESULT_VEHICLE_INFO[] entitys = bll.GetModelList("ID = " + strUniqueKey);
                if (entitys.Length > 0)
                {
                    bool IsMTC = IYAKASAPortal.Statistics.StaInfoTools.GetIsMTC(entitys[0].CLZLDH);
                    string strContent = string.Empty;
                    if (IsMTC)
                    {
                        strContent = "window.open('" + string.Format("PrintAuto_MotorYQ.aspx?Key={0}", strUniqueKey) + "');";
                    }
                    else
                    {
                        strContent = "window.open('" + string.Format("PrintAuto_AJYQ.aspx?Key={0}", strUniqueKey) + "');";
                    }

                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "uuu", strContent, true);
                }
            }
            catch (Exception ex)
            {

            }
        }
    }
}